/**
 * Created by CJB on 2017-01-03 10:52:25.
 */
var loadDiv = null;
$(function () {
	loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
    initUpdateWindow();
});

/**
 * 初始化更新窗口
 * @returns {Function}
 */
function initUpdateWindow(){
	var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
    zTree = zTree.setting.callback.onClick = onDialogTreeOnClick("dictionaryAddListEdit","parentIdEdit");
    //声明消息提示
    var toolTips = new Object();
    var validator = $("#updateForm").validate({
    	rules:{
    		code:{
    			required:true,
        		remote:{
        			url: getPath() +  "app/dictionary/validateCode",
        			type: "post",
        			data:{
        				newCode:function(){
    					return $("#labelEdit").val();
    					},
                        id:function(){
                            return $("#updateId").val();
                        }
        			}
        		}
    		},
    		
            name:"required",
            sortFlag:{
            	required:true,
	            digits:true
        	},
        	dictionaryAddList:"required"
        },
        messages:{
            code:{required:"请输入字典编码！",
        		remote: "已存在此编码，请重新输入！"},
        	name:"请输入字典名称",
        
        	sortFlag:{required:"请输入排序",digits:"只能输入数字"},
            authorityAddList:"请选择父节点"
        },
        errorPlacement: function(eror,element){
            var toolTip = createTooltip(toolTips,element.attr("id"));
            toolTip.setContent(eror.text());
            toolTip.close();
            element.addClass("error-border");

        },
        success:function(label){
            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
            var toolTip = toolTips[elementId];
            $("#" + elementId).removeClass("error-border");
            toolTip.close();
            toolTip.detach();
        },
        submitHandler:function(form){
        	if($("#parentIdEdit").val() == $("#updateId").val()){
                createWindow('父节点不能是当前节点').dialog('open');
                return ;
            }
        	loadDiv.showLoading();
        	post(getPath() + "app/dictionary/edit",
                    $("#updateForm").serialize(),
                    function(resultObject){
        				loadDiv.hideLoading();
                        var errors = resultObject.errors;
                        //判断后台是否有返回错误消息
                        if(errors && errors.length > 0){
                            for(var i=0; i < errors.length; i++){
                                var error = errors[i];
                                var toolTip = createTooltip(toolTips,error.field);
                                toolTip.setContent(error.defaultMessage);
                                $("#" + error.field).addClass("error-border");
                            }
                        }else{
                            //直接更改和移动弹出框的树形数据
                            var node1 = getNodeById('parentMenuTree', $("#updateId").val());
                            var parentNode = getNodeById('parentMenuTree', $("#parentIdEdit").val());
                            node1.name = $("#permissionEdit").val();
                            var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
                            zTree.updateNode(node1);
                            var mo = zTree.moveNode(parentNode,node1,'inner');
                            //直接更改和移动左边的树形数据
                            var node2 = getNodeById('menuTree', $("#updateId").val());
                            var menuparentNode = getNodeById('menuTree', $("#parentIdEdit").val());
                            node2.name = $("#permissionEdit").val();
                            var zTree = $.fn.zTree.getZTreeObj('menuTree');
                            zTree.updateNode(node2);
                            mo = zTree.moveNode(menuparentNode,node2,'inner');
                            createWindow("更新成功").dialog("open");
                        	//createWindow("更新成功",{url: getPath() + "app/dictionary/list"}).dialog("open");
                        }
                    },function(resultObject){
                    	loadDiv.hideLoading();
    	        		createWindow(resultObject).dialog('open');
    	        	})
            }
        });
/*  //隐藏列表，显示添加
	$(".grid-list").show();
	$('.grid-list').hide();
	//取消
	$('#cancleBtn').click(function(){
		$(".grid-list").hide();
		$('.grid-list').show();
	});*/
        $("#dictionaryAddListEdit").click({menuContentId:"parentMenu"}, showMenu);
       }
   